package com.bizzard.org.controller;

import com.bizzard.basic.annotation.PreAuthorize;
import com.bizzard.basic.util.AjaxResult;
import com.bizzard.basic.util.PageList;
import com.bizzard.org.domain.Department;
import com.bizzard.org.query.DepartmentQuery;
import com.bizzard.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类
 */

@RestController
@RequestMapping("/department")
@Api(value = "部门接口",description = "部门接口")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     *  查询单个对象接口
     * @param id
     * @return
     */
    @ApiOperation(value = "查询单个对象")
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个对象",sn = "department:getOne")
    public Department findOne (@PathVariable("id")Long id){
        return  departmentService.findById(id);

    }

    /**
     *  查询所有对象接口
     * @return
     */
    @ApiOperation(value = "查询所有对象")
    @GetMapping
    @PreAuthorize(name = "查询所有对象",sn = "department:getAll")
    public List<Department> findAll (){
        return  departmentService.findAll();

    }

    /**
     *  分页查询接口
     * @param departmentQuery
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    @PreAuthorize(name = "部门列表",sn = "department:list")
    public PageList<Department> queryPage (@RequestBody DepartmentQuery departmentQuery){
        return  departmentService.queryPage(departmentQuery);

    }

    /**
     *  删除操作接口
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "部门删除",sn = "department:delete")
    public AjaxResult delete (@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }

    }




    /**
     *  添加和修改 接口
     */
    @PutMapping
    @ApiOperation(value = "添加或修改")
    @PreAuthorize(name = "部门保存和修改",sn = "department:save")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() == null){//添加
                departmentService.add(department);
            }else{
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    @PreAuthorize(name = "批量删除",sn = "department:patchDelete")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
     * 查询部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树-无限极接口")
    @PreAuthorize(name = "部门树",sn = "department:deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }





}
